package com.educate.book.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import com.educate.book.model.bo.BookBO;
import com.educate.book.model.po.Book;
import com.educate.common.mongo.QueryUtils;
import com.educate.common.web.PageableUtils;

public class BookDaoImpl {

    @Resource MongoTemplate mongoTemplate;

    public Page<Book> query(BookBO bo, Pageable pageable) {
        List<Criteria> andCriteria = new ArrayList<>(10);
//        Criteria keyCriteria = QueryUtils.keyWord(keyWord, "qq", "wx", "tb");
//        if (keyCriteria != null) {
//            andCriteria.add(keyCriteria);
//        }
//        if (startTime != null || endTime != null) {
//            Criteria createTime = Criteria.where("createTime");
//            if (startTime != null) {
//                createTime.gt(startTime);
//            }
//            if (endTime != null) {
//                createTime.lt(endTime);
//            }
//            andCriteria.add(createTime);
//        }
        if (!StringUtils.isEmpty(bo.getISBN())) {
        	andCriteria.add(Criteria.where("ISBN").regex("^.*" +bo.getISBN()+ ".*$"));
        }
        if (!StringUtils.isEmpty(bo.getBookName())) {
        	andCriteria.add(Criteria.where("bookName").regex("^.*" +bo.getBookName()+ ".*$"));
        }
        if (!StringUtils.isEmpty(bo.getBookNameEn())) {
        	andCriteria.add(Criteria.where("bookNameEn").regex("^.*" +bo.getBookNameEn()+ ".*$"));
        }
        if (!StringUtils.isEmpty(bo.getAuthor())) {
        	andCriteria.add(Criteria.where("author").regex("^.*" +bo.getAuthor()+ ".*$"));
        }
        if (!StringUtils.isEmpty(bo.getBriefIntroduction())) {
        	andCriteria.add(Criteria.where("briefIntroduction").regex("^.*" +bo.getBriefIntroduction()+ ".*$"));
        }
        if (!StringUtils.isEmpty(bo.getStatus())) {
        	andCriteria.add(Criteria.where("status").is(bo.getStatus()));
        }
        if (!StringUtils.isEmpty(bo.getFeatured())) {
        	andCriteria.add(Criteria.where("featured").is(bo.getFeatured()));
        }
        if (!StringUtils.isEmpty(bo.getRecommend())) {
        	andCriteria.add(Criteria.where("recommend").is(bo.getRecommend()));
        }
        if (!StringUtils.isEmpty(bo.getBookNumber())) {
            andCriteria.add(Criteria.where("bookNumber").is(bo.getBookNumber()));
        }
        Query query = QueryUtils.query(andCriteria).with(pageable);
        query.with(new Sort(new Order(Direction.DESC, "recommend"), new Order(Direction.DESC, "crateTime")));
        List<Book> list = mongoTemplate.find(query, Book.class);
        return PageableUtils.getPage(list, pageable, () -> mongoTemplate.count(query, Book.class));
    }
}
